<?php
define('DSN', 'mysql:host=localhost;dbname=lab20');
define('DB_USER', 'root');
define('DB_PWD', '123456');

$title = $_REQUEST['term'];

try{
	$pdo = new PDO(DSN, DB_USER, DB_PWD);
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$pdo->exec("set names utf8");
}
catch(Exception $e){
	echo $e->getMessage();
}

try{
	$sql = "SELECT title as label,id,time FROM `news` WHERE `title` LIKE :title";
	$stmt = $pdo->prepare($sql);
	$title = '%'.$title.'%';
	$stmt->bindParam(":title", $title);
	$stmt->execute();
	$news = $stmt->fetchAll(PDO::FETCH_ASSOC);
	echo json_encode($news);
}
catch(PDOException $e){
	echo $e->getMessage();
}